package com.sixth.controller.wx;

import com.sixth.bean.MarketBrand;
import com.sixth.bean.MarketCoupon;
import com.sixth.bean.bo.CouponPageInfo;
import com.sixth.bean.bo.TopicPageInfo;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.*;
import com.sixth.service.*;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lixinhang
 * @Time 2022/9/9 10:27
 */
@RestController
@RequestMapping("wx/home")
public class WXHomeController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    CouponService couponService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    AdvService advService;
    @Autowired
    AdminBrandService adminBrandService;
    @Autowired
    TopicService topicService;

    /**
     * @author lixinhang
     * @return
     * 新增进入小程序首页，展示商品、类目、优惠券等的handler,好一手高耦合低内聚的代码
     */
    @RequestMapping("index")
    public BaseRespVo wxHomeIndex() {
        //查询前8条isNew为true的商品，组成newGoodsList
        List<WXHomeIndexGoodsVO> newGoodsList = goodsService.indexGoodsList("isNew",new BasePageInfo(1,8,"id","desc"));

        //查询前三张可用的优惠券，组成couponList
        CouponPageInfo couponPageInfo = new CouponPageInfo();
        couponPageInfo.setStatus(0);        //
        couponPageInfo.setPage(1);          //一页
        couponPageInfo.setLimit(3);         //3条
        couponPageInfo.setSort("add_time"); //按照新增时间排序
        couponPageInfo.setOrder("desc");    //降序排序
        List<MarketCoupon> list = couponService.getCouponList(couponPageInfo).getList();
        List<WXHomeIndexCouponVO> couponList = new ArrayList<>();
        for (MarketCoupon marketCoupon : list) {
            WXHomeIndexCouponVO wxHomeIndexCouponVO = new WXHomeIndexCouponVO();
            try {
                BeanUtils.copyProperties(wxHomeIndexCouponVO,marketCoupon);
            } catch (Exception e) {
                e.printStackTrace();
            }
            couponList.add(wxHomeIndexCouponVO);
        }

        //查询全部的类目，组成channel
        List<WXHomeIndexChannelVO> channel = categoryService.indexChannel();

        //查询全部的广告，组成banner
        List<WXHomeIndexBannerVO> banner =  advService.indexBanner();

        //按照id排序，查询前四条brand  组成brandList  我也不想写硬编码
        Object marketBrands = adminBrandService.list(new BasePageInfo(1, 4, "id", "asc"), null, null);
        BasePageInfoVo basePageInfoVo = (BasePageInfoVo)marketBrands;
        List<MarketBrand> marketBrandList  = basePageInfoVo.getList();
        List<WXHomeIndexBrandVO> brandList = new ArrayList<>();
        for (MarketBrand marketBrand : marketBrandList) {
            //TODO 如果后续图片地址变化，那么这行代码也要变化；
            WXHomeIndexBrandVO wxHomeIndexBrandVO = new WXHomeIndexBrandVO(marketBrand.getFloorPrice(), marketBrand.getPicUrl(), marketBrand.getName(), marketBrand.getId(), marketBrand
                    .getDesc());
            brandList.add(wxHomeIndexBrandVO);
        }

        //查询前6条isHot为true的商品，组成hotGoodsList
        List<WXHomeIndexGoodsVO> hotGoodsList = goodsService.indexGoodsList("isHot", new BasePageInfo(1, 6, "add_time", "desc"));

        //查询一条topic,组成topicList
        TopicPageInfo topicPageInfo = new TopicPageInfo();
        topicPageInfo.setPage(1);
        topicPageInfo.setLimit(1);
        topicPageInfo.setSort("add_time");
        topicPageInfo.setOrder("desc");
        BasePageInfoVo basePageInfoVO = (BasePageInfoVo) topicService.getTopicList(topicPageInfo);
        List<MarketTopicVO> marketTopicVOList = basePageInfoVO.getList();
        List<WXHomeIndexTopicVO> topicList = new ArrayList<>();

        for (MarketTopicVO marketTopicVO : marketTopicVOList) {
            WXHomeIndexTopicVO wxHomeIndexTopicVO = new WXHomeIndexTopicVO();
            try {
                BeanUtils.copyProperties(wxHomeIndexTopicVO,marketTopicVO);
            } catch (Exception e) {
                e.printStackTrace();
            }
            topicList.add(wxHomeIndexTopicVO);
        }

        //查询5个一级类目， 查到一级目录后，得到其二级目录列表ids, 查询5件商品，组成floorGoodsList
        List<WXHomeIndexFloorGoodsVO> floorGoodsList =  categoryService.indexFloorGoodsList();

        WXHomeIndexVO wxHomeIndexVO = new WXHomeIndexVO(newGoodsList, couponList, channel, banner, brandList, hotGoodsList, topicList, floorGoodsList);

        return BaseRespVo.ok(wxHomeIndexVO);
    }

}
